package com.boilerCloud.service.maintenance;

import java.time.LocalDate;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.boilerCloud.dao.maintenance.MaintenanceDao;
import com.boilerCloud.dao.work.WorkOrderDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.maintenance.MaintenancePlan;
import com.boilerCloud.dto.work.WorkOrder;
import com.boilerCloud.util.RedisPoolsUtil;
import com.boilerCloud.util.StringUtil;

@Service
public class MaintenanceServiceImpl implements IMaintenanceService {

	@Resource
	private MaintenanceDao dao;
	
	@Resource
	private WorkOrderDao orderdao;
	
	@Override
	public void qryList(MaintenancePlan model, HzlqswPage<MaintenancePlan> page)
			throws Exception {
		dao.qryList(model,page);	
	}

	@Override
	public void edit(MaintenancePlan model) throws Exception {
		String startTime = model.getStartTimeStr();
		int maintenanceTime = model.getMaintenanceTime();//保内维保
		String[] time_array = startTime.split("-");
		LocalDate startDate = LocalDate.of(Integer.parseInt(time_array[0]), Integer.parseInt(time_array[1]), Integer.parseInt(time_array[2]));
		LocalDate finshDate = null ;
		int maintenanceType = model.getMaintenanceType();//1天2周3月4年
		if(maintenanceType == 1){
			finshDate = startDate.plusDays(maintenanceTime);
		}
		if(maintenanceType == 2){
			finshDate = startDate.plusWeeks(maintenanceTime);
		}
		if(maintenanceType == 3){
			finshDate = startDate.plusMonths(maintenanceTime);
		}
		if(maintenanceType == 4){
			finshDate = startDate.plusYears(maintenanceTime);
		}
		LocalDate now = LocalDate.now();
		int type = 2;
		if(now.isBefore(finshDate)){//在保期内
			type = 1;
		}
		int status = 2;//待派单
		
		LocalDate firstDate = null; //下次维保时间
		
		int maintenanceCycleType = model.getMaintenanceCycleType();//1天2周3月4年
		int maintenanceCycle =model.getMaintenanceCycle();
		if(maintenanceCycleType == 1){
			firstDate = startDate.plusDays(maintenanceCycle);
		}
		if(maintenanceCycleType == 2){
			firstDate = startDate.plusWeeks(maintenanceCycle);
		}
		if(maintenanceCycleType == 3){
			firstDate = startDate.plusMonths(maintenanceCycle);
		}
		if(maintenanceCycleType == 4){
			finshDate = startDate.plusYears(maintenanceCycle);
		}
		
		LocalDate firstDate_order = null;
		int advanceDispatch = model.getAdvanceDispatch();
		if(advanceDispatch < 4){
			firstDate_order = firstDate.plusDays(0 - advanceDispatch);
		}else if(advanceDispatch < 6){
			firstDate_order = firstDate.plusWeeks(0 - advanceDispatch);
		}else{
			firstDate_order = firstDate.plusMonths(0 - advanceDispatch);
		}
		if(now .isBefore(firstDate_order)){//开始维保时间 说明维保未开始
			status = 1;//
		}
		if(model.getAutomatic() == 1){
			if(type == 2) status = 4; //停止
		}
		model.setStatus(status);
		model.setType(type);
		model.setNextDate(firstDate);
		model.setNextDateOrder(firstDate_order);
		model.setFinshDate(finshDate);
		dao.edit(model);
		
	}

	@Override
	public MaintenancePlan getById(int id) throws Exception {
		
		return dao.getById(id);
	}

	@Override
	public void save(MaintenancePlan model) throws Exception {
		String startTime = model.getStartTimeStr();
		int maintenanceTime = model.getMaintenanceTime();//保内维保
		String[] time_array = startTime.split("-");
		LocalDate startDate = LocalDate.of(Integer.parseInt(time_array[0]), Integer.parseInt(time_array[1]), Integer.parseInt(time_array[2]));
		LocalDate finshDate = null ;
		int maintenanceType = model.getMaintenanceType();//1天2周3月4年
		if(maintenanceType == 1){
			finshDate = startDate.plusDays(maintenanceTime);
		}
		if(maintenanceType == 2){
			finshDate = startDate.plusWeeks(maintenanceTime);
		}
		if(maintenanceType == 3){
			finshDate = startDate.plusMonths(maintenanceTime);
		}
		if(maintenanceType == 4){
			finshDate = startDate.plusYears(maintenanceTime);
		}
		LocalDate now = LocalDate.now();
		int type = 2;
		if(now.isBefore(finshDate)){//在保期内
			type = 1;
		}
		int status = 2;//待派单
		
		LocalDate firstDate = null; //下次维保时间
		
		int maintenanceCycleType = model.getMaintenanceCycleType();//1天2周3月4年
		int maintenanceCycle =model.getMaintenanceCycle();
		if(maintenanceCycleType == 1){
			firstDate = startDate.plusDays(maintenanceCycle);
		}
		if(maintenanceCycleType == 2){
			firstDate = startDate.plusWeeks(maintenanceCycle);
		}
		if(maintenanceCycleType == 3){
			firstDate = startDate.plusMonths(maintenanceCycle);
		}
		if(maintenanceCycleType == 4){
			finshDate = startDate.plusYears(maintenanceCycle);
		}
		
		LocalDate firstDate_order = null;
		int advanceDispatch = model.getAdvanceDispatch();
		if(advanceDispatch < 4){
			firstDate_order = firstDate.plusDays(0 - advanceDispatch);
		}else if(advanceDispatch < 6){
			firstDate_order = firstDate.plusWeeks(0 - advanceDispatch);
		}else{
			firstDate_order = firstDate.plusMonths(0 - advanceDispatch);
		}
		if(now .isBefore(firstDate_order)){//开始维保时间 说明维保未开始
			status = 1;//
		}
		if(model.getAutomatic() == 1){
			if(type == 2) status = 4; //停止
		}
		model.setStatus(status);
		model.setType(type);
		model.setNextDate(firstDate);
		model.setNextDateOrder(firstDate_order);
		model.setFinshDate(finshDate);
		  /**  维保时间类型 1天2周3月4年  *//*
		  *//**  维保周期  *//*
		  private int maintenanceCycle;
		  *//**  提前派单时间 1天2周3月4年  *//*
		  private int advanceDispatch;
		  *//**  维保时间类型 1=1天2 =2天3=3天 4=一周 5=2周 6=一个月  *//*
		  private int maintenanceCycleType;*/
		dao.save(model);
		
	}

	@Override
	public void del(int id) throws Exception {
		dao.del(id);
		
	}

	@Override
	public void saveWorkOrder(WorkOrder model) throws Exception {
		String workNo = "W"+StringUtil.DateToString(new Date(), 2) + RedisPoolsUtil.getCounter("work_counter");
		model.setOrderNo(workNo);
		model.setStatus(1);
		model.setOrderType(1);
		model.setMaintenanceId(model.getId());
		orderdao.save(model);
		dao.updateStatus(model.getId(),3);
		//orderdao.save(model);
		
	}


}
